import * as api from './api';
import {
    dict,
    UserPageQuery,
    AddReq,
    DelReq,
    EditReq,
    CrudOptions,
    CreateCrudOptionsProps,
    CreateCrudOptionsRet
} from '@fast-crud/fast-crud';
import {dictionary} from '/@/utils/dictionary';

export const createCrudOptions = function ({crudExpose, context}: CreateCrudOptionsProps): CreateCrudOptionsRet {

    let filename = NaN

    const pageRequest = async (query: UserPageQuery) => {
        return await api.GetList(query);
    };
    const delRequest = async ({row}: DelReq) => {
        return await api.DelObj(row.id);
    };
    const addRequest = async ({form}: AddReq) => {
        const data = crudExpose!.getSearchFormData()
        const parent = data.parent
        form.kid = parent
        form.name = filename
        if (parent) {
            return await api.AddObj(form);
        } else {
            return undefined
        }

    };

    return {
        crudOptions: {
            request: {
                pageRequest,
                addRequest,
                delRequest,
            },
            rowHandle: {
                //固定右侧
                fixed: 'right',
                width: 200,
                buttons: {
                    view: {
                        show: false,
                    },
                    remove: {
                        iconRight: 'Delete',
                        type: 'text',
                    },
                },
            },
            columns: {
                _index: {
                    title: '序号',
                    form: {show: false},
                    column: {
                        //type: 'index',
                        align: 'center',
                        width: '70px',
                        columnSetDisabled: true, //禁止在列设置中选择
                        formatter: (context) => {
                            //计算序号,你可以自定义计算规则，此处为翻页累加
                            let index = context.index ?? 1;
                            let pagination = crudExpose!.crudBinding.value.pagination;
                            // @ts-ignore
                            return ((pagination.currentPage ?? 1) - 1) * pagination.pageSize + index + 1;
                        },
                    },
                },                
                kid: {
                    title: 'kid',
                    type: 'input',
                    column: {
                        show: false,
                    },
                    form: {
                        show: false,
                    },
                },
                name: {
                    title: '文件名',
                    search: {
                        show: true,
                        component: {
                            props: {
                                clearable: true,
                            },
                        },
                    },
                    type: 'input',
                    form: {
                        show: false,
                    },
                },
                type: {
                    title: '类型',
                    search: {
                        show: false
                    },
                    type: 'dict-select',
                    dict: dict({
                        data: [
                            {
                                label: '文档',
                                value: 1,
                            },
                            {
                                label: '接口',
                                value: 2,
                            }
                        ],
                    }),
                    form: {
                        show: false,
                    },
                },                
                status: {
                    title: '状态',
                    search: {
                        show: true
                    },
                    type: 'dict-select',
                    dict: dict({
                        data: [
                            {
                                label: '失败',
                                value: 0,
                            },
                            {
                                label: '成功',
                                value: 1,
                            },
                            {
                                label: '处理中',
                                value: 2,
                            }
                        ],
                    }),
                    form: {
                        show: false,
                    },
                },
                file: {
                    title: '文件',
                    type: "file-uploader",
                    form: {
                        component: {
                            multiple: true, //可选择多个
                            uploader: {
                                type: "form"
                            },
                            props: {
                                onSuccess: (res) => {
                                    console.log('upload success -> ', res)
                                    console.log('file -> ', filename)
                                    filename = res.res.name
                                    
                                    console.log('file -> ', filename)
                                },// 上传成功的回调
                            },

                        }
                    },
                    column: {
                        show: false,
                    }                    
                },
            },
        },
    };
};
